package com.csust.automaticteach.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.csust.automaticteach.common.BaseResponse;
import com.csust.automaticteach.common.ResultUtils;
import com.csust.automaticteach.exception.ErrorCode;
import com.csust.automaticteach.exception.ThrowUtils;
import com.csust.automaticteach.model.dto.exercisessets.ExercisesSetsQueryRequest;
import com.csust.automaticteach.model.entity.ExercisesSets;
import com.csust.automaticteach.model.vo.exercises.ExercisesVO;
import com.csust.automaticteach.service.ExercisesSetsService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * @ClassName ExercisesSetController
 * @Description 套题控制器
 * @Author钰玟
 * @Date 2025/7/23 下午9:04
 * @Version 1.0
 **/
@RestController
@RequestMapping("/exercisesSet")
public class ExercisesSetsController {

    @Resource
    private ExercisesSetsService exercisesSetsService;

    /**
     * 获取套题中的详细题目
     *
     * @param setId 套题Id
     * @return 套题中的详细题目列表
     */
    @GetMapping("/get")
    public BaseResponse<List<ExercisesVO>> getExercisesSets(@RequestParam("setId") Long setId) {
        //1.校验参数
        ThrowUtils.throwIf(setId == null || setId <= 0, ErrorCode.PARAMS_ERROR);
        //2.由服务层 获取
        List<ExercisesVO> exerciseVOList = exercisesSetsService.getExerciseVOList(setId);
        //3.返回结果
        return ResultUtils.success(exerciseVOList);
    }

    /**
     * 删除整套题目（并不是套题中的单个题目）
     *
     * @param setId 套题Id
     * @return 删除结果
     */
    @PostMapping("/delete")
    @Transactional
    public BaseResponse<Boolean> deleteExercisesSet(@RequestParam("setId") Long setId) {
        //1. 校验参数
        ThrowUtils.throwIf(setId == null || setId <= 0, ErrorCode.PARAMS_ERROR);
        //2. 由服务层 完成删除业务逻辑
        boolean b = exercisesSetsService.deleteExercisesSet(setId);
        //3. 返回结果
        return ResultUtils.success(b);
    }

    /**
     * 获取套题列表（分页）
     *
     * @param exercisesSetsQueryRequest 查询请求
     * @param request                   请求
     * @return 套题列表
     */
    @PostMapping("/list/vo/page")
    public BaseResponse<Page<ExercisesSets>> listExercisesSetByPage(@RequestBody ExercisesSetsQueryRequest exercisesSetsQueryRequest
            , HttpServletRequest request) {
        //1. 校验参数
        ThrowUtils.throwIf(exercisesSetsQueryRequest == null, ErrorCode.PARAMS_ERROR);
        //2. 由服务层 完成分页查询业务逻辑
        Page<ExercisesSets> exercisesSetsPage = exercisesSetsService.listExercisesSetByPage(exercisesSetsQueryRequest, request);
        //3. 返回结果
        return ResultUtils.success(exercisesSetsPage);
    }
}
